package dcpb.re.enc;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.AbstractMap.SimpleEntry;

import dcpb.re.encrypt.AES;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.plaf.jpbc.field.z.ZrElement;

public class Alice {

	private PubParams p;
	ProxyReEncrypt  d=new ProxyReEncrypt ();
	private List<Element> mu;
	private SimpleEntry<Map,Map> content;
	public Map ct;
	private int m,t;
	private byte[] encryptedMessage;
	public Alice(PubParams p,int m,int t) {
		// TODO Auto-generated constructor stub
		this.p=p;
		this.m=m;
		this.t=t;
	}
	public byte[] FileEncrypt(Element MSK,List<Element> mu,Element A) {
		 ZrElement M=(ZrElement) this.p.Zr.newRandomElement();// �ԳƼ��ܵ���Կ
		 System.out.println(M);
		 String message="hello world";// Ҫ���͵���Ϣ
		 encryptedMessage= AES.encrypt(message.getBytes(),M.toBytes());
		 //d.writeFile("./data/encryptedMessage",encryptedMessage);
		 ct=d.Enc(MSK,mu,this.p,M,p.one,A);//sita C
		return encryptedMessage;
		  
	}
	public Element RkGen(Element MSK,Element w){
		Element rk=p.h.duplicate().mulZn(p.one.duplicate().div(MSK.duplicate().add(w)));
//		Element A=null;
//		try {
//			A = p.Zr.newElementFromBytes(d.readFile("data/A"));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return rk;
	}
	public void dec() {
		byte[] m1=d.Dec1(p,content.getKey(),content.getValue());
		   System.out.println("DEC1-message:"+new BigInteger(m1).toString());
		   String message;
		try {
			message = new String(d.decrypt(encryptedMessage, new BigInteger(m1).toByteArray()));
			System.out.println(message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	public void setReceivers(List<Element> mu){
		this.mu=mu;
	}
	public List<Element> getReceivers() {
		return this.mu;
	}
	public List<Element> readIDFromFile(String path) throws IOException, ClassNotFoundException{
    	FileInputStream freader = new FileInputStream(path);
    	ObjectInputStream objectInputStream = new ObjectInputStream(freader);
    	List<byte[]> idbyte2=(List<byte[]>) objectInputStream.readObject();
    	List<Element> id2=new LinkedList<>();
    	for(byte[] bytes:idbyte2) {
    		id2.add(p.Zr.newElementFromBytes(bytes));
    	}
    	return id2;
    	
    }
	public static void main(String[] args) {
		ProxyReEncrypt  d=new ProxyReEncrypt ();
		PubParams p=null;
		try {
			p=d.read();// IO
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int m=SystemProperty.getIntegerValue("m");
		int t=SystemProperty.getIntegerValue("t");
		Alice a=new Alice(p,m,t);
		try {
			a.setReceivers(a.readIDFromFile("data/ids"));//IO to Fabric
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//a.FileEncrypt();
		
		//List<Proxy> ps=a.RkGen();
		//a.dec();
		
	}
	

}
